/***********************************************
 @Name :        小谭老师
 @Time :        2025-2-22
 @Email :       tan.zhipeng@outlook.com
 @Comment :     功能窗口 智能家居
 @FileName :    widget_funcc_smart_home.cpp
 @Version :     1.0
************************************************/

#include "widget_funcc_smart_home.h"
#include "ui_widget_funcc_smart_home.h"

Widget_funcC_Smart_Home::Widget_funcC_Smart_Home(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget_funcC_Smart_Home)
{
    ui->setupUi(this);
    //按钮初始化
    ui->label_return_home->setPixmap(QPixmap(":/状态按钮_关"));
    ui->label_run_home->setPixmap(QPixmap(":/状态按钮_关"));
    ui->window->setPixmap(QPixmap(":/状态按钮_关"));
    ui->curtains->setPixmap(QPixmap(":/状态按钮_关"));
    ui->cleaner->setPixmap(QPixmap(":/状态按钮_关"));
    ui->air_conditioner->setPixmap(QPixmap(":/状态按钮_关"));
    ui->bathtub->setPixmap(QPixmap(":/状态按钮_关"));
    ui->light->setPixmap(QPixmap(":/状态按钮_关"));
    ui->TCP->setIcon(QIcon(":/连接"));
    ui->script->setPixmap(QPixmap(":/状态按钮_关"));
    //图标初始化
    ui->window_icon->setPixmap(QPixmap(":/关窗"));
    ui->curtains_icon->setPixmap(QPixmap(":/关窗帘"));
    ui->cleaner_icon->setPixmap(QPixmap(":/扫地机器人"));
    ui->air_conditioner_icon->setPixmap(QPixmap(":/空调"));
    ui->bathtub_icon->setPixmap(QPixmap(":/浴缸接水"));
    ui->light_icon->setPixmap(QPixmap(":/关灯"));
    ui->go_home_icon->setPixmap(QPixmap(":/回家"));
    ui->go_out_icon->setPixmap(QPixmap(":/外出"));
    ui->TCP_icon->setPixmap(QPixmap(":/网络"));
    ui->script_icon->setPixmap(QPixmap(":/脚本"));
    // 启动自适应
    ui->window_icon->setScaledContents(true);
    ui->curtains_icon->setScaledContents(true);
    ui->cleaner_icon->setScaledContents(true);
    ui->air_conditioner_icon->setScaledContents(true);
    ui->bathtub_icon->setScaledContents(true);
    ui->light_icon->setScaledContents(true);
    ui->go_home_icon->setScaledContents(true);
    ui->go_out_icon->setScaledContents(true);
    ui->TCP_icon->setScaledContents(true);
    ui->script_icon->setScaledContents(true);
    //字体初始化
    font_1.setFamily("Microsoft YaHei"); // 设置字体系列，例如微软雅黑:cite[10]
    font_1.setPointSize(8);            // 设置字体大小
    font_1.setBold(true);               // 设置加粗
    // 设置字体
    ui->window_label->setFont(font_1);
    ui->curtains_label->setFont(font_1);
    ui->cleaner_label->setFont(font_1);
    ui->air_conditioner_label->setFont(font_1);
    ui->bathtub_label->setFont(font_1);
    ui->light_label->setFont(font_1);
    ui->go_home_label->setFont(font_1);
    ui->go_out_label->setFont(font_1);
    ui->TCP_label->setFont(font_1);
    //状态值初始化
    windos_statues=false;
    curtains_statues=false;
    light_statues=false;
    // 连接按钮信号
    connect(ui->label_return_home , &Button_home_start::signals_start , this , &Widget_funcC_Smart_Home::slots_button_start);
    connect(ui->label_run_home , &Button_home_start::signals_start , this , &Widget_funcC_Smart_Home::slots_button_start);

    //窗户部分
    connect(ui->window, &Button_home_start::signals_start, this, [this](){
        this->windos_statues=!this->windos_statues;
        if(this->windos_statues==true)
        {
            emit window_open();
        }
        else
        {
            emit window_close();
        }
    });
    // 在构造函数中添加这两行连接
    connect(this, &Widget_funcC_Smart_Home::window_open, this, [this](){
        ui->window_icon->setPixmap(QPixmap(":/开窗"));
        ui->window_label->setText("打开窗户");
    });

    connect(this, &Widget_funcC_Smart_Home::window_close, this, [this](){
        ui->window_icon->setPixmap(QPixmap(":/关窗")); // 确保有关窗图片
        ui->window_label->setText("关闭窗户");
    });
    //窗户部分

    //窗帘部分
    connect(ui->curtains, &Button_home_start::signals_start, this, [this](){
        this->curtains_statues=!this->curtains_statues;
        if(this->curtains_statues==true)
        {
            emit curtains_open();
        }
        else
        {
            emit curtains_close();
        }
    });
    connect(this, &Widget_funcC_Smart_Home::curtains_open, this, [this](){
        ui->curtains_icon->setPixmap(QPixmap(":/开窗帘"));
        ui->curtains_label->setText("打开窗帘");
    });
    connect(this, &Widget_funcC_Smart_Home::curtains_close, this, [this](){
        ui->curtains_icon->setPixmap(QPixmap(":/关窗帘"));
        ui->curtains_label->setText("关闭窗帘");
    });
    //窗帘部分

    //TCP部分
    connect(ui->TCP, &QPushButton::clicked, this, [this](){
            emit TCP_connect();
    });
    connect
            (this, &Widget_funcC_Smart_Home::TCP_connect, this, [this]()
                {
                    Widget *tcpWindow = new Widget();

                    // 设置关闭时自动删除，避免内存泄漏
                    tcpWindow->setAttribute(Qt::WA_DeleteOnClose);

                    // 显示窗口
                    tcpWindow->show();
                }
            );
    //窗帘部分

    //灯光部分
    connect(ui->light, &Button_home_start::signals_start, this, [this](){
        this->light_statues=!this->light_statues;
        if(this->light_statues==true)
        {
            emit light_open();
        }
        else
        {
            emit light_close();
        }
    });
    connect(this, &Widget_funcC_Smart_Home::light_open, this, [this](){
        ui->light_icon->setPixmap(QPixmap(":/开灯"));
        ui->light_label->setText("开启灯光");
    });
    connect(this, &Widget_funcC_Smart_Home::light_close, this, [this](){
        ui->light_icon->setPixmap(QPixmap(":/关灯"));
        ui->light_label->setText("关闭灯光");
    });
    //灯光部分
    //点击搜索按钮，开始查询歌曲列表，将当前歌曲切换为搜索歌曲
    connect(ui->widget_8, &search_music:: searchRequested, ui->widget_20,&music :: handle_search);
}

Widget_funcC_Smart_Home::~Widget_funcC_Smart_Home()
{
    delete ui;
}

void Widget_funcC_Smart_Home::slots_button_start(bool start)
{
    
    QObject * temp = sender();
    // 离开
    if(temp == ui->label_run_home)
    {
        // 离家开
        ui->label_run_home->set_start(start);
        // 归家关
        ui->label_return_home->set_start(!start);
    }
    // 归家
    if(temp == ui->label_return_home)
    {
        // 离家开
        ui->label_return_home->set_start(start);
        // 归家关
        ui->label_run_home->set_start(!start);
    }
}



